﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Manage_EditTrainReview : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            #region 加载Templates下面的模版
            string path = Server.MapPath(Toolkit.PathOfTemplates);
            var fs = from aF in (new DirectoryInfo(path)).GetFiles("*.aspx")
                     where !aF.Name.StartsWith("_")
                     select new { Name = aF.Name.Replace(".aspx", "") };
            ArticleTemplates.DataSource = fs.ToList();
            ArticleTemplates.DataValueField = "Name";
            ArticleTemplates.DataTextField = "Name";
            ArticleTemplates.DataBind();

            ArticleTemplates.Items[0].Selected = true;
            #endregion


            string tid = Request.Params["tid"];
            string aid = Request.Params["aid"];
            CmsDataContext db = new CmsDataContext();

            if (tid != null && tid != "")
            {
                TrainID.Value = tid;

                Train t = (from aT in db.Train
                           where aT.ID == tid
                           select aT).First();

                ArticleTitle.Text = Toolkit.ArticlePrefixOfReview +t.Name;
                ArticleSubtitle.Text = ArticleTitle.Text;
                ArticleKeywords.Text =Toolkit.ArticlePrefixOfReview.Substring(0, Toolkit.ArticlePrefixOfReview.Length-1);
                ArticleBody.Text = string.Format("<p>本次培训的主要内容是：</p>" +
                    "<p>{0}</p>" +
                    "<hr/><p>以下是本次培训的精彩图片：</p>" +
                    "<p>精彩图片1</p><p>精彩图片2</p><p>...</p><p>精彩图片n</p>", t.Memo);
            }
            else if (aid != null && aid != null)
            {
                ArticleID.Value = aid;

                Article article = (from aA in db.Article
                                   where aA.ID == aid
                                   select aA).First();

                ArticleTitle.Text = article.Title;
                ArticleSubtitle.Text = article.Subtitle;
                ArticleKeywords.Text = article.Keywords;
                ArticleBody.Text = article.Body;
            }
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        CmsDataContext db = new CmsDataContext();
        string id = ArticleID.Value.Trim();
        Article article;

        try
        {
            if (id == "")
            {
                article = new Article();
                article.ID = Toolkit.NextUUID;
                article.Clicked = 0;
                article.District = Toolkit.CurrentUserDistrict;
                article.Belong = Toolkit.CurrentUserBelong;
                article.Published = DateTime.Now;
                article.TopicID = Toolkit.TopicIdOfTrainReview;
                article.Footer = "";

                article.VerifyID = Toolkit.CurrentUserID;
                article.Verified = DateTime.Now;


                db.Article.InsertOnSubmit(article);

                #region 更新当前培训的ArticleID字段
                Train train = (from aT in db.Train
                               where aT.ID == TrainID.Value.Trim()
                               select aT).First();
                train.ReviewArticleID = article.ID;
                #endregion
            }
            else
            {
                article = (from aA in db.Article
                           where aA.ID == id
                           select aA).First();
            }

            article.Title = ArticleTitle.Text.Trim();
            article.Subtitle = ArticleSubtitle.Text.Trim();
            article.Keywords = ArticleKeywords.Text.Trim();
            article.Body = ArticleBody.Text.Trim();
            article.Template = ArticleTemplates.SelectedValue;

            DateTime dt = article.Published;
            int year = dt.Year;
            int month = dt.Month;

            article.Url = string.Format("{0}/{1}/{2}.html", year, month, article.ID);

            if (article.CreatorID == null)
            {
                article.CreatorID = Convert.ToString(Session["ManagerID"]);
            }

            db.SubmitChanges();

            Toolkit.StaticArticle(article.Url, article.Template);

            MessageBox.Message = "培训回顾保存成功。";
            MessageBox.Location = "./EditArticle.aspx?aid=" + article.ID;
        }
        catch (Exception ee)
        {
            MessageBox.Message = ee.Message;
        }
    }
    protected void VerifyArticle_Click(object sender, EventArgs e)
    {
        CmsDataContext db = new CmsDataContext();

        try
        {
            Article article = (from aA in db.Article
                               where aA.ID == ArticleID.Value.Trim()
                               select aA).First();

            article.VerifyID = Toolkit.CurrentUserID;
            article.Verified = DateTime.Now;

            db.SubmitChanges();

            MessageBox.Message = "文章已经通过审核。";
            MessageBox.Location = "./EditArticle.aspx?aid=" + article.ID;
        }
        catch (Exception ee)
        {
            MessageBox.Message = ee.Message;
        }
    }
}